
/* COPYRIGHT (C) 2012 Ori Arad. All Rights Reserved. */
/* See LICENSE.txt for more details                  */

package edu.technion.cse.simpleGoogleMaps;

import java.awt.Component;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.net.URL;
import javax.imageio.ImageIO;
import javax.swing.JFrame;

/**
 * This class provide a simple API to Google-Maps services.
 * It allows the user to generate a Google-Maps image, or a URL
 * to be used in a browser. 
 * @author Ori Arad
 * @version 0.01 14/07/12 (DD/MM/YY)
 * @todo Add street-view support:
 *       http://maps.googleapis.com/maps/api/streetview?size=600x300&location=(48.856206, 2.2976810000000114)&heading=315.47499999999997&fov=90&pitch=23.275000000000002&sensor=false
 *       http://maps.googleapis.com/maps/api/streetview?size=400x400&location=40.720032,%20-73.988354&fov=90&heading=235&pitch=10&sensor=false
 *       https://developers.google.com/maps/documentation/streetview/
 *       http://gmaps-samples.googlecode.com/svn/trunk/streetview/streetview_lazy.html
 *       http://gmaps-samples.googlecode.com/svn/trunk/
 *       http://maps.google.com/intl/iw/help/maps/streetview/learn/where-is-street-view.html       
 */
public class SimpleGoogleMaps extends Component {

	private static final long serialVersionUID = 1L;
	public static final int ROADMAP = 0;
    public static final int SATELLITE = 1;
    public static final int TERRAIN = 2;
    public static final int HYBRID = 3;	
    private static final int DEFAULT_WIDTH = 400;
    private static final int DEFAULT_HEIGHT = 400;
    private static final String[] MAPTYPE = {"roadmap", "satellite", "terrain", "hybrid"};
    private JFrame frame;
    private BufferedImage img;
    private Dimension dimension;
    private int zoom = 12;
    private double latitude = 0.0;
    private double longitude= 0.0;
    private int scale = 1;
	private int mapType = ROADMAP;
    private String windowTitle = "SimpleGoogleMaps";
    
    public int getScale() {
		return scale;
	}

	public void setScale(int scale) {
		this.scale = scale;
	}

	public int getMapType() {
		return mapType;
	}

	public void setMapType(int mapType) {
		this.mapType = mapType;
	}
    
    public Dimension getDimension() {
		return dimension;
	}

	public void setDimension(Dimension dimension) {
		this.dimension = dimension;
	}
	
    public int getWidth() {
		return (int)dimension.getWidth();
	}

	public void setWidth(int width) {
		this.dimension.setSize(width, getHeight());
	}
	
    public int getHeight() {
		return (int)dimension.getHeight();
	}

	public void setHeight(int height) {
		this.dimension.setSize(getWidth(), height);
	}

	public int getZoom() {
		return zoom;
	}

	public void setZoom(int zoom) {
		this.zoom = zoom;
	}

	public double getLatitude() {
		return latitude;
	}

	public void setLatitude(double latitude) {
		this.latitude = latitude;
	}

	public double getLongitude() {
		return longitude;
	}

	/**
	 * @param longitude the longitude to set
	 */
	public void setLongitude(double longitude) {
		this.longitude = longitude;
	}	

	/**
	 * @return the windowTitle
	 */
	public String getWindowTitle() {
		return windowTitle;
	}

	/**
	 * @param windowTitle the windowTitle to set
	 */
	public void setWindowTitle(String windowTitle) {
		this.windowTitle = windowTitle;
	}	

	/**
	 * @return
	 * TODO Consider rename to toString
	 */
	public String generateUrl() {
    	StringBuilder str = new StringBuilder("http://maps.googleapis.com/maps/api/staticmap?center=");
    	str.append(latitude);
    	str.append(",");
    	str.append(longitude);
    	str.append("&zoom=");
    	str.append(zoom);
    	str.append("&size=");
    	str.append((int)dimension.getHeight());
    	str.append("x");
    	str.append((int)dimension.getWidth());
    	str.append("&maptype=");
    	str.append(MAPTYPE[mapType]);    	    	
    	str.append("&scale=");
    	str.append(scale);    	
    	str.append("&language=iw&sensor=false");  
    	// TODO Add free text
    	// TODO Add language option
    	// TODO Add markers & style    	

    	return str.toString();
    }

    public void paint(Graphics g) {
        g.drawImage(img, 0, 0, null);
    }

    public Dimension getPreferredSize() {
        if (img == null) {
             return new Dimension((int)dimension.getWidth(), (int)dimension.getHeight());
        } else {
           return new Dimension(img.getWidth(null), img.getHeight(null));
       }
    }	
	
	public SimpleGoogleMaps() {
        frame = new JFrame(windowTitle);
        dimension = new Dimension(DEFAULT_WIDTH, DEFAULT_HEIGHT);
                      
        frame.addWindowListener(new WindowAdapter(){
                public void windowClosing(WindowEvent e) {
                    System.exit(0);
                }
            });

        try {
        	URL url = new URL(generateUrl());
        	img = ImageIO.read(url);
        } catch (IOException e) {
        }
        
        frame.add(this);
        frame.pack();
        frame.setVisible(true);		
	}
	
	public void refresh() {
        try {
        	URL url = new URL(generateUrl());
        	img = ImageIO.read(url);
        } catch (IOException e) {
        	e.printStackTrace();        	
        }
        frame.setTitle(windowTitle);
        frame.setPreferredSize(dimension);
        frame.repaint();
        frame.pack();
	}
}
